package org.jverify;

import java.util.List;
import java.util.Properties;

import javax.mail.MessagingException;

import org.jverify.utility.smtp.MailSender;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class EmailAlerter implements Alerter {

	private final Properties settings;
	private final RunMode runMode;
	private static final Logger LOG = LoggerFactory.getLogger(EmailAlerter.class);

	public EmailAlerter(final Properties settings, final RunMode runMode) {
		this.settings = settings;
		this.runMode = runMode;
	}

	@Override
	public void sendAlert(final List<String> alerts) {
		try {
			final StringBuilder body = new StringBuilder("Checksum failures \n\r");
			for (final String fileName : alerts) {
				body.append(fileName);
				if (runMode == RunMode.REPLACE) {
					body.append(" : Attempted database restore");
				}
				body.append("\n\r");
			}
			MailSender.sendMail(settings.getProperty("to"), null, settings.getProperty("from"),
					settings.getProperty("smtpserver"), "Altered files found", body.toString());
		}
		catch (final MessagingException e) {
			LOG.info(e.getLocalizedMessage());
		}
	}
}
